/** 
 * @Title: Gzip.java 
 * @Package gzip 
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月2日 下午6:33:52 
 */
package com.zizo.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipException;

/**
 * @ClassName: Gzip
 * @Description: TODO
 * @author HeYan
 * @date 2016年9月2日 下午6:33:52
 * 
 */
public class Gzip {

	/**
	 * 文件解压缩
	 * 
	 * @param file
	 * @param delete
	 *            是否删除原始文件
	 * @throws Exception
	 */
	public static String decompress(String pathname) throws FileNotFoundException {
		File file = new File(pathname);
		FileInputStream fis = new FileInputStream(file);
		GZIPInputStream gis;
		try {
			gis = new GZIPInputStream(fis);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			int count;
			byte data[] = new byte[1024];
			while ((count = gis.read(data)) > 0) {
				out.write(data, 0, count);
			}
			String res = out.toString("gb2312");// gb1808
			out.close();
			gis.close();
			fis.close();
			return res;
		} catch (ZipException e) {
			try {
				@SuppressWarnings("resource")
				BufferedReader br = new BufferedReader(new FileReader(file));
				StringBuffer sb = new StringBuffer();
				String temp = "";
				while ((temp = br.readLine()) != null) {
					sb.append(temp);
				}
				return sb.toString();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) throws Exception {

	}
}
